package com.vanysoft.business.sys.service.impl;

import com.baomidou.mybatisplus.plugins.Page;
import com.vanysoft.business.sys.dao.UserInfoMapper;
import com.vanysoft.business.sys.entity.Menu;
import com.vanysoft.business.sys.entity.Org;
import com.vanysoft.business.sys.entity.UserInfo;
import com.vanysoft.business.sys.service.MenuService;
import com.vanysoft.business.sys.service.OrgService;
import com.vanysoft.business.sys.service.RoleService;
import com.vanysoft.business.sys.service.UserInfoService;
import com.vanysoft.common.utils.desc.DescUtil;
import com.vanysoft.common.utils.string.StringUtil;
import com.vanysoft.framework.config.database.BaseServiceImpl;
import com.vanysoft.framework.exception.VanySoftBaseException;
import com.vanysoft.framework.messagesource.MessageCodeConstant;
import com.vanysoft.framework.redis.RedisService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;
import java.util.Map;
import java.util.Objects;

import static com.vanysoft.framework.constants.CommonConstant.IS_NOT_ENABLE;

/**
 * 用户服务类实现
 *
 * @author qi_ma
 */
@Service
public class UserInfoServiceImpl extends BaseServiceImpl<UserInfoMapper, UserInfo> implements UserInfoService {

    @Resource
    private RedisService redisService;

    @Resource
    private OrgService orgService;

    @Resource
    private MenuService menuService;

    @Resource
    private RoleService roleService;

    @Override
    public String doLogin(String username, String password) {
        if (StringUtil.isEmpty(username) || StringUtil.isEmpty(password)) {
            // 用户名或密码为空
            throw new VanySoftBaseException(MessageCodeConstant.LoginConstant.NAME_PWD_EMPTY);
        }
        // 1.查询用户信息
        UserInfo userInfo = this.selectByUsername(username);
        // 用户不存在
        if (userInfo == null) {
            throw new VanySoftBaseException(MessageCodeConstant.LoginConstant.NAME_PWD_ERROR);
        }
        //禁用
        if (Objects.equals(userInfo.getIsEnable(), IS_NOT_ENABLE)) {
            throw new VanySoftBaseException(MessageCodeConstant.LoginConstant.USER_STATUS_DISABLED);
        }
        // 2.验证用户密码
        boolean checkPwd = DescUtil.checkPassWord(password, userInfo.getPassword());
        if (!checkPwd) {
            throw new VanySoftBaseException(MessageCodeConstant.LoginConstant.NAME_PWD_ERROR);
        }
        // 用户主键
        String userId = userInfo.getId();
        // 3.查询用户其它
        //组织机构信息
        List<Org> orgs = orgService.selectByUserId(userId);
        userInfo.setOrgs(orgs);
        List<Menu> menus;
        if (userInfo.isAdmin()) {
            menus = menuService.selectAll(userId);
        } else {
            menus = menuService.selectByUserId(userId);
        }
        userInfo.setMenus(menus);

        return redisService.initUserInfoToCache(userInfo);
    }

    @Override
    public UserInfo selectByUsername(String username) {
        return baseMapper.selectByUsername(username);
    }

    @Override
    public void doLogout(String token) {
        redisService.deleteRedis(token);
    }

    @Override
    public Page<UserInfo> selectAll(Page<UserInfo> page) {
        page.setRecords(baseMapper.selectAll(page, page.getCondition()));
        return page;
    }

    @Override
    public List<UserInfo> selectAll(Map<String, Object> condition) {
        return baseMapper.selectAll(condition);
    }

    @Override
    public List<UserInfo> selectByRoleIds(String[] roleIds) {
        return baseMapper.selectByRoleIds(roleIds);
    }

    @Override
    public boolean saveFavorite(String userId, String menuId, Boolean isFavorite) {
        int i;
        if (isFavorite) {
            i = baseMapper.addFavorite(userId, menuId);
        } else {
            i = baseMapper.deleteFavorite(userId, menuId);
        }
        return retBool(i);
    }
}
